#pragma once

#include "JWTS_global.h"

class QString;

#define CP_ACP 0

int JWTS_EXPORT KMultiByteToWideChar(
        const unsigned int CodePage,
        const unsigned long dwFlags,
        const char *lpMultiByteStr,
        const int cbMultiByte,
        wchar_t *lpWideCharStr,
        const int cchWideChar
    );

int JWTS_EXPORT KWideCharToMultiByte(
        const unsigned int CodePage,
        const unsigned long dwFlags,
        const wchar_t *lpWideCharStr,
        const int cchWideChar,
        char *lpMultiByteStr,
        const int cbMultiByte,
        const char *lpDefaultChar,
        int *lpUsedDefaultChar
    );

template<class T>
int lstrcmpi(const T *s1, const T *s2)
{
    while ((*s1 != T(0)) || (*s2 != T(0)))
    {
        auto c1 = tolower(*s1++);
        auto c2 = tolower(*s2++);
        if (c1 < c2)
            return(-1);
        else if (c1 > c2)
            return(1);
    }
    return 0;
}

template<class T>
int strncmpi(const T *s1, const T *s2, const size_t count)
{
    for (size_t i = 0; i < count; ++i)
    {
        auto c1 = tolower(*s1++);
        auto c2 = tolower(*s2++);
        if (c1 < c2)
            return(-1);
        else if (c1 > c2)
            return(1);
        else if (c1 == 0)
            return(0);
    }
    return 0;
}

template<class T>
int strToUpperCmp(const T *s1, const T *s2, const size_t count)
{
    for (size_t i = 0; i < count; ++i)
    {
        auto c1 = toupper(*s1++);
        auto c2 = *s2++;
        if (c1 < c2)
            return(-1);
        else if (c1 > c2)
            return(1);
        else if (c1 == 0)
            return(0);
    }
    return 0;
}


template<class T>
void replaceChar(const int oldChar, const int newChar, T *p_string)
{
    while (*p_string != 0)
    {
        if (*p_string == oldChar)
            *p_string = newChar;

        ++p_string;
    }
}

enum ConvType
{
    ToCp1251,
    ToLocal8bit,
    ToUtf8
};

QString JWTS_EXPORT toQtStr(const wchar_t *pwString);
QString JWTS_EXPORT toQtStr(const char *pString, bool *isUtf8 = nullptr);
void JWTS_EXPORT fromQtStr(const QString &string, char *pString, const ConvType convType = ConvType::ToLocal8bit);


char JWTS_EXPORT *KCharUpper(char *lpsz);
wchar_t JWTS_EXPORT *KCharUpper(wchar_t *lpsz);

unsigned long JWTS_EXPORT AnsiLowerBuff(char *lpsz, unsigned long cchLength);
